//
//  XTStatus.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/16.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

class XTStatus: NSObject {

    //MARK: - 属性
    //
    //https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00COdAXDwQ4gdE391fb667f41SP6yD
    //微博创建时间
    var created_at : String?
    
    //微博ID
    var id : Int64 = 0
    
    //微博信息内容
    var text : String?
    
    //微博来源
    var source : String? {
        didSet {
            //Optional("<a href=\"http://app.weibo.com/t/feed/5iusLt\" rel=\"nofollow\">语录达人</a>")
            if source?.characters.count > 0 {
                //转成NSString
                let sourceStr = source! as NSString
        
                let firstRange = sourceStr.rangeOfString(">")
                let secondRange = sourceStr.rangeOfString("</")
                let range = NSRange(location: firstRange.location + 1, length: secondRange.location - firstRange.location - 1)
                
                source = sourceStr.substringWithRange(range)
            }
            
        
        }
        
    }
    
    //转发数
    var reposts_count : Int = 0
    
    //评论数
    var comments_count : Int = 0
    
    //表态数
    var attitudes_count : Int = 0
    
    //微博配图
    // thumbnail_pic":"http://ww2.sinaimg.cn/thumbnail/e9492957gw1f3wde5ykd3j20go0gomyp.jpg"}
    var pic_urls : [[String : AnyObject]]?{
        didSet {
            if let picArray = pic_urls {
            
                //创建一个空数组
                pictureURLs = [NSURL]()
                //遍历
                for dict in picArray {
            
                    if let urlStr = dict["thumbnail_pic"] as? String {
                    
                        let url = NSURL(string: urlStr)
                        //将转好的url放入数组中
                        pictureURLs?.append(url!)
                        
                    }
                }
            }
        
        }
    }
    
    //微博配图URL数组
    var pictureURLs : [NSURL]?
    
    
    //微博作者的用户信息
    var user : XTUserStatus?
    
    //转发微博
    var retweeted_status : XTStatus?
    
    
    
    
    //字典转模型
    init(dict : [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //判断  如果key 是user拦截下来,传入字典,转成模型
        if key == "user" {
            
            if let dict = value as? [String : AnyObject] {
            
                //字典转模型
               user = XTUserStatus(dict: dict)
            }
            
            //一定要return 不然执行到下面user这个属性又会被重置
            return
        }
        
        if key == "retweeted_status" {
        
            if let dict = value as? [String : AnyObject] {
            
                retweeted_status = XTStatus(dict: dict)
                
                return
            }
            
        }
        
        //调用父类
        super.setValue(value, forKey: key)
        
        
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    ///对象打印
    override var description : String {
        
        get {
        
            let keys = ["created_at","id","text","source","reposts_count","comments_count","attitudes_count","pic_ids", "user"]
            
            // \n 换行  \t table
            return "\n \t 微博模型 : \(dictionaryWithValuesForKeys(keys))"
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
